package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.annotation.UpdateConfig;
import com.yuandian.api.management.entity.ConfAlarmTemplate;
import com.yuandian.api.management.entity.ConfAlarmTemplateHis;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.admin.api.feign.RemoteDictService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.constant.DictType;
import com.yuandian.management.mapper.ConfAlarmTemplateMapper;
import com.yuandian.management.service.ConfAlarmTemplateHisService;
import com.yuandian.management.service.ConfAlarmTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 告警模板表-Service实现
 * @author zfsn
 * @date 2023-4-3
 */
@Service
public class ConfAlarmTemplateServiceImpl extends ServiceImpl<ConfAlarmTemplateMapper, ConfAlarmTemplate>
		implements ConfAlarmTemplateService {

	@Autowired
	ConfAlarmTemplateMapper confAlarmTemplateMapper;

	@Autowired
	ConfAlarmTemplateHisService confAlarmTemplateHisService;

	@Autowired
	RemoteDictService remoteDictService;

	/**
	 * 通过主键查询数据
	 *
	 * @param id 主键集合
	 * @return 通用返回对象
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ConfAlarmTemplate getById(Integer id) {
		ConfAlarmTemplate alarmTemplate = super.getById(id);
		for (SysDictItem alarmTemplateType : remoteDictService.getDictByType(DictType.CONF_ALARM_TEMPLATE_TYPE).getData()) {
			if (Objects.equals(alarmTemplateType.getItemValue(), String.valueOf(alarmTemplate.getType()))) {
				alarmTemplate.setTypeName(alarmTemplateType.getLabel());
			}
		}
		return alarmTemplate;
	}

	/**
	 * 分页查询
	 *
	 * @param confAlarmTemplate 筛选条件
	 * @return 通用返回对象
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Page<ConfAlarmTemplate> listPage(ConfAlarmTemplate confAlarmTemplate) {
		Page<ConfAlarmTemplate> confAlarmTemplatePage = confAlarmTemplateMapper.listPage(new Page<>(confAlarmTemplate.getPageNum(), confAlarmTemplate.getPageSize()), confAlarmTemplate);
		Map<String, String> alarmTemplateType = remoteDictService.getDictByType(DictType.CONF_ALARM_TEMPLATE_TYPE).getData().stream().collect(Collectors.toMap(SysDictItem::getItemValue, SysDictItem::getLabel));
		for (ConfAlarmTemplate alarmTemplate : confAlarmTemplatePage.getRecords()) {
			alarmTemplate.setTypeName(alarmTemplateType.get(String.valueOf(alarmTemplate.getType())));
		}
		return confAlarmTemplatePage;
	}

	/**
	 * 新增数据
	 *
	 * @param confAlarmTemplate 实例对象
	 * @return 通用返回对象
	 */
	@Override
	@UpdateConfig
	@Transactional(rollbackFor = Exception.class)
	public R<String> saveConfig(ConfAlarmTemplate confAlarmTemplate) {
		if (!checkAlarmTemplateDuplicates(confAlarmTemplate)){
			confAlarmTemplate.setCreateBy(SecurityUtils.getUser().getName());
			super.save(confAlarmTemplate);
			return R.ok("增加告警模板成功");
		}
		return R.failed("告警模板名称已存在");
	}

	/**
	 * 通过主键更新数据
	 *
	 * @param confAlarmTemplate 实例对象
	 * @return 通用返回对象
	 */
	@Override
	@UpdateConfig
	@Transactional(rollbackFor = Exception.class)
	public R<String> updateConfigById(ConfAlarmTemplate confAlarmTemplate) {
		if (!checkAlarmTemplateDuplicates(confAlarmTemplate)){
			confAlarmTemplate.setUpdateBy(SecurityUtils.getUser().getName());
			confAlarmTemplate.setUpdateTime(LocalDateTime.now());
			super.updateById(confAlarmTemplate);

			ConfAlarmTemplateHis confAlarmTemplateHis = new ConfAlarmTemplateHis();
			confAlarmTemplateHis.setTemplateId(confAlarmTemplate.getId());
			confAlarmTemplateHis.setContent(confAlarmTemplate.getContent());
			confAlarmTemplateHis.setDescription(confAlarmTemplate.getDescription());
			confAlarmTemplateHis.setCreateTime(LocalDateTime.now());
			confAlarmTemplateHisService.save(confAlarmTemplateHis);
			return R.ok();
		}
		return R.failed();
	}

	/**
	 * 修改是否启用状态
	 *
	 * @param confAlarmTemplate
	 * @return 结果
	 */
	@Override
	@UpdateConfig
	@Transactional(rollbackFor = Exception.class)
	public R<String> updateTemplateEnable(ConfAlarmTemplate confAlarmTemplate) {
		UpdateWrapper<ConfAlarmTemplate> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", confAlarmTemplate.getId());
		updateWrapper.set("is_enable", confAlarmTemplate.getIsEnable());
		updateWrapper.set("update_by", SecurityUtils.getUser().getName());
		super.update(updateWrapper);
		ConfAlarmTemplateHis confAlarmTemplateHis = new ConfAlarmTemplateHis();
		confAlarmTemplateHis.setTemplateId(confAlarmTemplate.getId());
		confAlarmTemplateHis.setContent("  ");
		confAlarmTemplateHis.setDescription("  ");
		confAlarmTemplateHis.setCreateTime(LocalDateTime.now());
		confAlarmTemplateHisService.save(confAlarmTemplateHis);
		return R.ok();
	}

	/**
	 * 判断模板名称是否重复
	 * @param  confAlarmTemplate
	 */
	private boolean checkAlarmTemplateDuplicates(ConfAlarmTemplate confAlarmTemplate) {
		return confAlarmTemplateMapper.checkAlarmTemplateDuplicates(confAlarmTemplate) > 0;
	}

}